$(function() {  
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        Rk.FecharModal();
    });     
    
    $('.restauranteBar .rangaki').click(function(){location.href="/restaurantes"});
    
    $('.onde_retirar').click(function(e){
      e.preventDefault();
      window.scrollTo(0);
      $('.endereco-restaurante')
        .animate({opacity:0.3}, 300, null, function(){
          $(this).addClass("focus");
          $(this).animate({opacity:1}, 300, null, function(){
            $(this).animate({opacity:0.3}, 300, null, function(){
              $(this).removeClass("focus");
              $(this).animate({opacity:1}, 300);
              });
            });
          });
    });
    
    $('input[name="retirada"]').change(function(){
      calculaTotal();
    });
    
    $('input[name="forma_pagamento"]').change(function(){
      var id = "#fp_" + $(this).val();
      $('.divFP').hide();
      $(id).show();
    });
    
    $('#troco').numberbox({
      min:0,
      precision:2,
      decimalSeparator:',',
      groupSeparator:'.',
      onChange: function(newVal, oldVal){
        var troco = newVal - calculaTotal();
        $('.vlrTroco').html(Rk.formataMoeda(troco));
      }
    });
    
    $('.enviar').click(function(){
      dados = {forma_pagamento:$('input[name="forma_pagamento"]').val(),
               troco:$('#troco').numberbox("getValue"),
               retirada:$('input[name="retirada"]').val(),
               p_id:$('#p_').val(),
               r_id:$('#r_').val()};
      Rk.service('Pedido','enviarPedido',dados,function(ret){
        if(ret){
          $('.pedidoId').html(ret);
          Rk.Modal('#pedido');
        }
        else{
          alert('ocorreu um erro');
        }
      });
    });
    
    $('.pagamento .forma-pagamento input').first().attr('checked', 'checked');
    $('.pagamento .forma-pagamento input').first().change();
    
    $('.voltar').click(function(){
      var url = "/" + $('#u_').val() + "/" + $('#r_').val();  
      window.location.href=url;
    });
    
    $('.acompanhar').click(function(){
      window.location.href="/meu";
    });
    
    $('.novo').click(function(){
      window.location.href="/restaurantes";
    });
    
    calculaTotal();
    
});

function calculaTotal(){
  var taxa = 1 * $('#taxa_entrega').attr('val');
  if($('#retirada').attr('checked')=='checked')
  {
    taxa = 0;
  }
  var total = 1 * $('#preco_total').attr('val');
  total += taxa;
  
  
  $('#taxa_entrega').html(Rk.formataMoeda(taxa));
  $('#preco_total').html(Rk.formataMoeda(total));
  return total;
}
